// Copyright 2011 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
#include <SDL.h>
#include <emscripten.h>


#define LINE 1000
int w = 800, h = 600;
void mainloop(void* arg) {
    SDL_Event event;
    while(SDL_PollEvent(&event)){}
    if (event.type==SDL_QUIT) return;
    
    SDL_Renderer* renderer = static_cast<SDL_Renderer*>(arg);
    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
    for(int i=0;i<LINE;i++){
        SDL_RenderDrawLine(renderer,i%w,rand()%h,i%w,rand()%h);
    }
    SDL_RenderPresent(renderer);// 交换缓冲区
}
int main(int argc, char** argv) {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* win = SDL_CreateWindow("sdl",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,w,h,SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);    
    SDL_Renderer *renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    emscripten_set_main_loop_arg(mainloop,renderer,-1,1);

    printf("hello world\nthis is a sdl draw program use wasm\n");
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);
    SDL_Quit();
    return 0;
}